package edu.tsinghua.mrpvc.device.mobile;

import java.util.Calendar;
import java.util.Hashtable;
import java.util.Map;

import org.gmote.server.GmoteServer;
import org.gmote.server.GmoteServerUi;
import org.gmote.server.GmoteServerUiWindows;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.EventHandler;
import org.osgi.util.tracker.ServiceTracker;

import edu.tsinghua.mrpvc.device.MobileDevice;
import edu.tsinghua.mrpvc.device.TvDevice;

public class MobileDeviceImpl implements MobileDevice, EventHandler {

	private static final String DEFAULT_SHARE_PATH = "c:/";
	private String sharePath;
	private String ID;
	private String owner;
	private ServiceTracker tracker;
	private BundleContext context;
	public MobileDeviceImpl(BundleContext context, String ID, String owner){
		this.context = context;
		this.ID = ID;
		this.owner = owner;
		sharePath = DEFAULT_SHARE_PATH;
		tracker = new ServiceTracker(context, EventAdmin.class.getName(), null);
		tracker.open();
	}
	public MobileDeviceImpl(BundleContext context)
	{this.context = context;}
    public void setSharePath(String path){
		this.sharePath = path;
	}
	@Override
	public String getOwner() {
		// TODO Auto-generated method stub
		return owner;
	}
    public void startMain(){
    	String []args={""};
		GmoteServer server = new GmoteServer(context,"phone","my");
		    GmoteServerUi ui = new GmoteServerUiWindows(server);
		    ui.sharedMain(args); 
    	
    }
    
	@Override
	public String getSharePath() {
		// TODO Auto-generated method stub
		return sharePath;
	}

	@Override
	public String getID() {
		// TODO Auto-generated method stub
		return ID;
	}

	@Override
	public boolean isValid() {
		// TODO Auto-generated method stub
		return true;
	}

	public void ctrlMouse(String action, String locationX, String locationY){
		EventAdmin eventAdmin = (EventAdmin) tracker.getService();
		if(eventAdmin != null){
			Map<String, String> proper = new Hashtable<String, String>();
			if(locationX.equals("100") && locationY.equals("100")){
				System.out.println(Calendar.getInstance().getTime().toLocaleString() +
					" Mobile Phone: control Tv and clicked \"OPEN DOOR\" button");
			}
			else if(locationX.equals("200") && locationY.equals("200")){
				System.out.println(Calendar.getInstance().getTime().toLocaleString() +
					" Mobile Phone: control Tv and clicked \"OPEN Picture File\" button");
			}
			proper.put(TvDevice.EVENT_PROPER_ACTION, action);
			proper.put(TvDevice.EVENT_PROPER_LOCATION_X, locationX);
			proper.put(TvDevice.EVENT_PROPER_LOCATION_Y, locationY);
			Event event = new Event(TvDevice.EVENT_TOPIC_CTRL_MOUSE, proper);
			eventAdmin.postEvent(event);
		}
	}
	
	public void ctrlKeyBoard(String key){
		EventAdmin eventAdmin = (EventAdmin) tracker.getService();
		if(eventAdmin != null){
			Map<String, String> proper = new Hashtable<String, String>();
			proper.put(TvDevice.EVENT_PROPER_KEY, key);
			Event event = new Event(TvDevice.EVENT_TOPIC_CTRL_KEY_BOARD, proper);
			eventAdmin.postEvent(event);
		}
	}
	
	public void bcSharePath(){
		EventAdmin eventAdmin = (EventAdmin) tracker.getService();
		if(eventAdmin != null){
			System.out.println(Calendar.getInstance().getTime().toLocaleString() + 
					" Mobile Phone: set share and post event");
			Map<String, String> proper = new Hashtable<String, String>();
			proper.put(MobileDevice.EVENT_PROPER_PATH, sharePath);
			Event event = new Event(MobileDevice.EVENT_TOPIC_SHARE_PATH, proper);
			eventAdmin.postEvent(event);
		}
	}
	@Override
	public void handleEvent(Event event) {
		// TODO Auto-generated method stub
		if(event.getTopic().equals(MobileDevice.EVENT_TOPIC_ISVALID)){
			EventAdmin eventAdmin = (EventAdmin) tracker.getService();
			if(eventAdmin != null){
				Map<String, String> proper = new Hashtable<String, String>();
				proper.put(MobileDevice.EVENT_PROPER_ID, ID);
				proper.put(MobileDevice.EVENT_PROPER_OWNER, owner);
				Event backEvent = new Event(MobileDevice.EVENT_TOPIC_STATE, proper);
				eventAdmin.postEvent(backEvent);
			}
		}
	}
	
	public void ctrlTv(String ctrlType){
		ServiceReference ref = context.getServiceReference(TvDevice.class.getName());
		if(ref != null){
			TvDevice tv = (TvDevice) context.getService(ref);
			if(ctrlType.equals("UpChannel")){
				System.out.println(Calendar.getInstance().getTime().toLocaleString() + 
						" Mobile Phone: control Tv UpChannel");
				tv.upChannel();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if(ctrlType.equals("PowerOn")){
				System.out.println(Calendar.getInstance().getTime().toLocaleString() + 
				" Mobile Phone: control Tv powerOn");
				tv.powerOn();
				
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
